package com.kong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kong.constant.MessageConstant;
import com.kong.entity.PageResult;
import com.kong.entity.QueryPageBean;
import com.kong.entity.Result;
import com.kong.pojo.CheckItem;
import com.kong.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项控制层
 */
@RestController  //@Controller + @ResponseBody
@RequestMapping("/checkitem")
public class CheckItemController {
    //推荐使用扫描方式
    @Reference
    private CheckItemService checkItemService;


    /**
     * 查询所有检查项 不分页  默认：GET POST
     */
    @GetMapping(value = "/findAll")
    public Result findAll(){
        try {
            List<CheckItem> checkItemList =  checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @PostMapping(value = "/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult byPage = checkItemService.findByPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,byPage);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping(value = "/addCheckItem")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemService.addCheckItem(checkItem);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @GetMapping(value = "/deleteCheckItem")
    public Result deleteCheckItem(Integer id){
        try {
            checkItemService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (RuntimeException r){
            return new Result(false,r.getMessage());
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @GetMapping(value="/findById")
    public Result findById(Integer id){
        try {
            CheckItem checkItem=checkItemService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @PostMapping(value = "/updateCheckItem")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemService.updateCheckItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

}
